import numpy as np
import pandas as pd
import cv2 as cv
from matplotlib import pyplot as plt
costume = pd.read_csv('/Volumes/Samsung_T3/project-repos/majorstudio/data/clothes/costume_reclass.csv')
# print(costume.head())
# Read the 1) image names and 2) image urls
label = costume['img']
url = costume['src']
# Check output
print(len(label))
print(label[0:5])
print(url[0:5])
import urllib
for i in range(len(url)):
name = label[i]
print(name)
link = url[i]
urllib.request.urlretrieve (link,'/Volumes/Samsung_T3/project-repos/majorstudio/img/'+ name)
import os
import numpy as np
# Create array with names of all images downloaded
jpg = []
for root, dirs, files in os.walk('/Volumes/Samsung_T3/project-repos/majorstudio/img'):
for file in files:
jpg.append(file)
print(len(jpg))
# Save array to csv
np.savetxt("jpg.csv", jpg,fmt="%s",delimiter=",")
for i in range(7110,len(label)):
name= label[i]
path = '/Volumes/Samsung_T3/project-repos/majorstudio/img/' + name
path1 = str(path)
print(path1)
img = cv.imread(path1)
# img = cv.blur(img,(2,2))
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
height, width = img.shape[:2]
rect = (1,1,width-5,height-5)
cv.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
tmp = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
_,alpha = cv.threshold(tmp,0,255,cv.THRESH_BINARY)
b, g, r = cv.split(img)
rgba = [b,g,r, alpha]
dst = cv.merge(rgba,4)
plt.imshow(dst),plt.colorbar(),plt.show()
cv.imwrite('/Volumes/Samsung_T3/project-repos/majorstudio/png/'+ name + '.png',dst)